package cz.acies.http.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cz.acies.data.process.AbstractProcessor;

public class DataConnector extends HttpServlet
{
	private static final long serialVersionUID = 165091856329475031L;

	public DataConnector() {
		super();
	}

	public void init() throws ServletException {}

	public void destroy() { super.destroy(); }

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = null;
		try {
			response.setContentType("text/plain");
			response.setCharacterEncoding("UTF-8");
			
			
			Map<String, Object> params = new HashMap<String, Object>();
			Enumeration<String> e = request.getParameterNames();
			while (e.hasMoreElements()) {
				String key =  e.nextElement();
				params.put(key, request.getParameter(key));
			}
			String modulId = request.getParameter("modulId");
			String data = "";//"P�i zpracov�n� dotazu do�lo k chyb�.";
			System.out.println("params: " + params); 
			if (modulId != null) {
				if (modulId.equals("LX_USER")) {
				} else if (modulId.equals("LX_PERSON")) {
					//AbstractProcessor processor = new PersonProcessor();
					//processor.execute(request, response);
				} 
			} else {
				data = "Pri zpracovani dotazu doslo k chybe.";
				out = response.getWriter();
				out.print(data);
				out.flush();
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		} finally {
			try { if (out != null) out.close(); } catch (Exception e) {}
		}
	}

}
